<template>
  <div class="app-container">
    <el-button type="primary" icon="el-icon-plus" @click="addList.isEdit = 0;addAccount()">增加</el-button>
    <el-table :data="list">
      <el-table-column label="序号">
        <template v-slot="scope">{{ scope.$index }}</template>
      </el-table-column>
      <el-table-column label="账户名" prop="username"></el-table-column>
      <el-table-column label="用户姓名" prop="chinese_name"></el-table-column>
      <el-table-column label="角色" prop="role"></el-table-column>
      <el-table-column label="职务" prop="zhiwu"></el-table-column>
      <el-table-column label="部门" prop="bumen"></el-table-column>
      <el-table-column label="事业部" prop="sbu"></el-table-column>
      <el-table-column label="操作" fixed="right" width="100">
        <template v-slot="scope">
          <el-button @click="addList.isEdit = 1;addAccount(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="delAccount(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="增加用户账户" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addList" label-width="80px">
        <el-form-item label="用户名">
          <template v-if="addList.isEdit == 0">
            <el-input v-model="addList.username" placeholder="请输入用户名"></el-input>
          </template>
          <span v-else>{{addList.username}}</span>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="addList.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="用户姓名">
          <el-input v-model="addList.chinese_name" placeholder="请输入用户中文姓名"></el-input>
        </el-form-item>
        <el-form-item label="权限角色">
          <el-input v-model="addList.role" placeholder="请输入‘管理员’或‘普通用户’"></el-input>
        </el-form-item>
        <el-form-item label="职务">
          <el-input v-model="addList.zhiwu" placeholder="请输入职务"></el-input>
        </el-form-item>
        <el-form-item label="部门">
          <el-input v-model="addList.bumen" placeholder="请输入部门"></el-input>
        </el-form-item>
        <el-form-item label="事业部">
          <el-input v-model="addList.sbu" placeholder="请输入事业部"></el-input>
        </el-form-item>
        <el-button type="primary" @click="addFormVisible = false; addList.isEdit = 0">取消</el-button>
        <el-button type="primary" @click="addSubmit();addFormVisible = false">增加</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getaccountlist, addaccountlist, delaccountlist } from "@/api/table";

export default {
  data() {
    return {
      list: null,
      addList: {
        isEdit: 0,
        username: null,
        password: null,
        role: null,
        chinese_name: null,
        zhiwu: null,
        bumen: null,
        sbu: null
      },
      listLoading: true,
      addFormVisible: false,
      needfresh: false
    };
  },
  watch: {
    needfresh: function() {
      this.fetchData();
      this.needfresh = false;
    }
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getaccountlist().then(response => {
        this.list = response.data.items;
        this.listLoading = false;
      });
    },
    addAccount(row) {
      this.addFormVisible = true;
      if (this.addList.isEdit === 1) {
        this.addList = Object.assign({}, row);
        this.addList.isEdit = 1;
      }
    },
    addSubmit() {
      addaccountlist(this.addList).then(response => {
        if (response.data.success === 1) {
          this.$message({
            message: "提交成功",
            type: "success"
          });
          this.addList.isEdit = 0;
          this.needfresh = true;
        }
      });
    },
    delAccount(row) {
      delaccountlist(row.username).then(response => {
        if (response.data.success == 1) {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.addList.isEdit = 0;
          this.needfresh = true;
        }
      });
    }
  }
};
</script>
